package com.itheima.controller;

import com.itheima.aspect.Log;
import com.itheima.entity.PageBean;
import com.itheima.entity.Result;
import com.itheima.entity.Student;
import com.itheima.entity.StudentParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 分页查询
     * @param studentParam
     * @return
     */
    @GetMapping
    public Result page(StudentParam studentParam){
        log.info("StudentParam={}", studentParam);
        //查询
        PageBean p= studentService.page(studentParam);
        return Result.success(p);
    }

    /**
     * 删除学生
     * @param ids
     * @return
     */
    @Log
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable Integer[] ids){
        log.info("ids:{}", Arrays.toString(ids));

        studentService.delete(ids);
        return Result.success();
    }

    /**
     * 添加学生
     * @param student
     * @return
     */
    @Log
    @PostMapping
    public Result save(@RequestBody Student student){
        log.info("student:{}",student);

        studentService.save(student);
        return Result.success();
    }

    /**
     * 根据id回显数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("id = {}", id);
        //查询
        Student student=studentService.getById(id);
        return Result.success(student);
    }

    /**
     * 修改学员信息
     * @param student
     * @return
     */
    @Log
    @PutMapping
    public Result update(@RequestBody  Student student){
        log.info("student:{}",student);

        studentService.update(student);
        return Result.success();
    }

    /**
     * 添加违纪分数
     * @param id
     * @param score
     * @return
     */
    @Log
    @PutMapping("/violation/{id}/{score}")
    public Result add(@PathVariable Integer id,@PathVariable Integer score){

        studentService.add(id,score);
        return Result.success();
    }
}
